package com.caineng.zhaobiao.zbapi.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.caineng.zhaobiao.zbapi.utils.AjaxResult;
import com.caineng.zhaobiao.zbapi.utils.SmsSampleUtils;
import com.caineng.zhaobiao.zbapi.utils.config.ConfigYml;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * api服务
 * @author liandyao
 * @date 2022/7/22 8:53
 */
@Slf4j
@Component
public class CainengApi {

    @Autowired
    private  ConfigYml confgYml ;
    /**
     * 登录验证
     * @param username
     * @param password
     * @return
     */
    public JSONObject login(String username, String password){
        String url = confgYml.getApiUrl()+"/login";
        JSONObject json = new JSONObject();
        json.putOpt("phone",username);
        json.putOpt("password",password);
        String res= HttpUtil.post(url,json,6000) ;
        JSONObject resu = JSONUtil.parseObj(res);
        log.info(resu.toString());
        return resu ;
    }

    /**
     * 获取验证码
     * @param phone
     * @return
     */
    public AjaxResult getSMS(String phone){
        /*String url = confgYml.getApiUrl()+"/getSms";
        log.info(url);

        JSONObject json = new JSONObject();
        json.putOpt("phone",phone);
        String res= HttpUtil.post(url,json,6000) ;
        JSONObject resu = JSONUtil.parseObj(res);
        log.info(resu.toString());
        */
        AjaxResult result = SmsSampleUtils.sendSms(phone);

        return result ;
    }

    /**
     * 校验验证码
     * @param phone
     * @return
     */
    public AjaxResult checkSMS(String phone,String code){
       /* String url = confgYml.getApiUrl()+"/checkSms";

        JSONObject json = new JSONObject();
        json.putOpt("phone",phone);
        json.putOpt("code",code);
        String res= HttpUtil.post(url,json,6000) ;
        JSONObject resu = JSONUtil.parseObj(res);
        log.info(resu.toString());*/
        AjaxResult result = SmsSampleUtils.checkCode(phone,code);
        return result ;
    }





    public static void main(String[] args) {
//        JSONObject obj = login("13999999999","123456");
//        System.out.println(obj);//{"msg":"成功","code":0,"data":{"showName":"","haveNotice":0,"password":"e10adc3949ba59abbe56e057f20f883e","phone":"13999999999","id":"103"}}
//        String userId = obj.getJSONObject("data").getStr("id");
//        System.out.println(userId);
//        //{"msg":"获取成功","code":0,"data":{"showName":"湖南建筑科技有限公司","headImg":"","enterprise":1,"idname":"","cardDown":"","trueName":"","haveNotice":0,"isDisable":0,"password":"","phone":"13999999999","approve":20,"integral":855,"cardUp":"","idcard":"","id":"103","isDrop":0,"invite":"CAINENG12","userType":30,"timStatus":1}}
//        JSONObject obj2 = getUserInfo(userId);
//        System.out.println(obj2);
    }

}
